package models

import (
	"github.com/jinzhu/gorm"
	"kop/libs/database"
	"kop/modules/activity/entity"
)

func GetTreasGuildRanking(guildID int32) *entity.TreasGuildRanking {

	var data = entity.TreasGuildRanking{GuildID: guildID}
	if err := database.DB.First(&data).Error; err != nil {
		if err == gorm.ErrRecordNotFound {
			return nil
		}
		panic(err)
	}

	return &data
}

func AddTreasGuildScore(guildID int32, score int64) error {

	return database.DB.Model(&entity.TreasGuildRanking{}).Where("guild_id = ?", guildID).
		UpdateColumn("score", gorm.Expr("score + ?", score)).Error
}

func SaveTreasGuildRanking(data *entity.TreasGuildRanking) {

	if err := database.DB.Save(data).Error; err != nil {
		panic(err)
	}
}
